package com.cooperate.fly.controller;

import com.cooperate.fly.web.Result;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by Stellar_Lee on 2016/4/18.
 */
public class BaseController {
    @Resource
    private MessageSource messageSource;

    @ExceptionHandler
    @ResponseBody
    public Result exception(Exception e){
        String msg=null;
        if(e instanceof IllegalArgumentException){
            msg=e.getMessage();
        }else{
            msg="system error";
        }
        String message=null;
        try{
            message=this.messageSource.getMessage(msg,null,null);
        }catch (NoSuchMessageException e1){
            message=msg;
        }
        return new Result(message);
    }
}
